import Variables from '../constant/vars';
import { IGlobalConfig, IScheduleResource } from '../interface';
import { GlobalConfigController } from './global-config.controller';

/**
 * @description 界面坐标系控制器
 * @export
 * @class UICoordinateController
 */
export class UICoordinateController {
  /**
   * @description 坐标元素
   * @private
   * @type {(HTMLDivElement | null)}
   * @memberof UICoordinateController
   */
  private coordinateElement: HTMLDivElement | null = null;

  /**
   * @description 全局配置控制器
   * @private
   * @type {GlobalConfigController}
   * @memberof UICoordinateController
   */
  private configController!: GlobalConfigController;

  /**
   * @description 所有单元格位置数据
   * @type {Array<{startX:number,startY:number}>}
   * @memberof UICoordinateController
   */
  allCells: Array<{ startX: number; startY: number }> = [];

  /**
   * @description 总天数
   * @type {number}
   * @memberof UICoordinateController
   */
  totalDays: number = 0;

  /**
   * @description 单元格宽度
   * @type {number}
   * @memberof UICoordinateController
   */
  cellWidth: number = 0;

  /**
   * @description 单元格高度
   * @type {number}
   * @memberof UICoordinateController
   */
  cellHeight: number = 0;

  /**
   * @description 画布宽度
   * @type {number}
   * @memberof UICoordinateController
   */
  canvasWidth: number = 0;

  /**
   * @description 画布高度
   * @type {number}
   * @memberof UICoordinateController
   */
  canvasHeight: number = 0;

  /**
   * Creates an instance of UICoordinateController.
   * @param {GlobalConfigController} configController
   * @memberof UICoordinateController
   */
  constructor(configController: GlobalConfigController) {
    this.configController = configController;
  }

  /**
   * @description 计算画布大小
   * @private
   * @param {IGlobalConfig} config
   * @param {IScheduleResource[]} resources
   * @returns {*}  {void}
   * @memberof UICoordinateController
   */
  private computeCanvasSize(
    config: IGlobalConfig,
    resources: IScheduleResource[],
  ): void {
    if (!this.coordinateElement) return;
    this.canvasWidth = this.coordinateElement.clientWidth;
    this.canvasHeight = Math.max(
      this.coordinateElement.clientHeight,
      resources.length * config.resourceBodyRowHeight,
    );
  }

  /**
   * @description 计算单元格大小
   * @private
   * @param {IGlobalConfig} config
   * @memberof UICoordinateController
   */
  private computeCellSize(config: IGlobalConfig): void {
    this.cellHeight =
      config.resourceBodyRowHeight /
      ((config.scaleRange[1] - config.scaleRange[0]) / config.scaleValue);
    const startDate = new Date(config.startTime);
    startDate.setHours(0, 0, 0, 0);
    const endDate = new Date(config.endTime);
    endDate.setHours(0, 0, 0, 0);
    this.totalDays = Math.ceil(
      (endDate.getTime() - startDate.getTime()) /
        Variables.time.millisecondOf.day,
    );
    const availableWidth =
      this.canvasWidth - config.resourceColumnWidth - config.scaleColumnWidth;
    this.cellWidth = availableWidth / this.totalDays;
  }

  /**
   * @description 构建坐标
   * @param {HTMLDivElement} coordinateElement
   * @param {IScheduleResource[]} resources
   * @memberof UICoordinateController
   */
  buildCoordinate(
    coordinateElement: HTMLDivElement,
    resources: IScheduleResource[],
  ): void {
    this.coordinateElement = coordinateElement;
    const config = this.configController.getConfig();
    this.computeCanvasSize(config, resources);
    this.computeCellSize(config);
  }

  /**
   * @description 销毁
   * @memberof UICoordinateController
   */
  destory(): void {}
}
